luogu1169 [ZJOI2007]棋盘制作

1
2
3
整张图可以重新分为两类
然后悬线法解决
极大子正方形和极大子矩阵
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
const int N=2008;
int n,m,tmp;
bool a[N][N];
int h[N][N],l[N][N],r[N][N];
int ans1,ans2;
int main()
{
n=read();m=read();
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
a[i][j]=read();
if((i+j)%2==0){
a[i][j]^=1;
}
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(a[i][j]==0){
h[i][j]=0;
l[i][j]=0;
}
else{
h[i][j]=h[i-1][j]+1;
l[i][j]=l[i][j-1]+1;
}
}
for(int j=m;j;--j){
if(a[i][j]==0){
r[i][j]=0;
}
else{
r[i][j]=r[i][j+1]+1;
}
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(i>1&&a[i-1][j]==1){
l[i][j]=min(l[i][j],l[i-1][j]);
r[i][j]=min(r[i][j],r[i-1][j]);
}
tmp=min(h[i][j],l[i][j]+r[i][j]-1);
ans1=max(ans1,tmp*tmp);
ans2=max(ans2,h[i][j]*(l[i][j]+r[i][j]-1));
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(a[i][j]==1){
h[i][j]=0;
l[i][j]=0;
}
else{
h[i][j]=h[i-1][j]+1;
l[i][j]=l[i][j-1]+1;
}
}
for(int j=m;j;--j){
if(a[i][j]==1){
r[i][j]=0;
}
else{
r[i][j]=r[i][j+1]+1;
}
}
}
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(i>1&&a[i-1][j]==0){
l[i][j]=min(l[i][j],l[i-1][j]);
r[i][j]=min(r[i][j],r[i-1][j]);
}
tmp=min(h[i][j],l[i][j]+r[i][j]-1);
ans1=max(ans1,tmp*tmp);
ans2=max(ans2,h[i][j]*(l[i][j]+r[i][j]-1));
}
}
printf("%d\n%d",ans1,ans2);
return 0;
}